package com.example.hospital.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.example.hospital.api.common.R;
import com.example.hospital.api.controller.from.LoginFrom;
import com.example.hospital.api.service.MisUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @FileName 心民医疗管理端接口
 * @folder
 * @Description
 * @Author yf
 * @return
 * @date 2024-08-16
 **/
@RestController
@RequestMapping("/mis_user")
@Api(tags = "心民医疗管理端接口")
public class MisUserController {
    @Resource
    private MisUserService misUserService;
    /**
    *
    * @folder 用户登录
    * @Param: [from]
    * @Author: yf
    * @return: com.example.hospital.api.common.R
    */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public R login(@RequestBody  @Valid LoginFrom from){
        Map param = BeanUtil.beanToMap(from);
        Integer userId = misUserService.login(param);
        if(userId != null){
            StpUtil.login(userId);
            String token = StpUtil.getTokenValue();
            List<String> permissions = StpUtil.getPermissionList();
            System.out.println(permissions);
            return R.ok().put("result",true).put("token",token).put("permissions",permissions);
        }
        return R.ok().put("result",false);
    }
    /**
    *
    * @folder 心民医疗系统管理端用户登出
    * @Param: []
    * @Author: yf
    * @return: com.example.hospital.api.common.R
    */

    @GetMapping("/logout")
    @ApiOperation("用户登出")
    @SaCheckLogin
    public R logout(){
        StpUtil.logout();
        return R.ok();
    }
}
